package ru.simplemaps.osmapi.providers;

import org.xml.sax.SAXException;
import ru.simplemaps.map.elements.Point;
import ru.simplemaps.map.provider.MapElementProvider;
import ru.simplemaps.osmapi.FullOSMService;
import ru.simplemaps.osmapi.OSMData;
import ru.simplemaps.osmapi.common.BBox;
import ru.simplemaps.osmapi.objects.Node;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author enaku_adm
 * @since 29.07.2010 16:53:44
 */
public class PointElementProvider implements MapElementProvider<Point> {
    private final FullOSMService service;

    public PointElementProvider(FullOSMService service) {
        this.service = service;
    }

    public Collection<Point> load(BBox box) throws IOException, SAXException, ParserConfigurationException {
        OSMData data = service.getDataByBbox(box);

        Collection<Node> nodes = data.nodes.values();
        List<Point> points = new ArrayList<Point>(nodes.size());
        for (Node n : nodes) {
            points.add(new Point(n.lat, n.lon));
        }

        return points;
    }
}
